package com.ruiyin.controller;

import com.ruiyin.entity.Valid;
import com.ruiyin.service.ValidService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("api")
public class ValidController extends BaseController {
    @Autowired
    private ValidService service;

    @RequestMapping(value = "valid")
    @ResponseBody
    public Object valid(Valid valid) {

        if (StringUtils.isBlank(valid.getImei())) {
            return sendFailed("非法访问！");
        }

        try {
            service.insertAccount(valid);
        } catch (Exception e) {
            return sendFailed("服务器异常，请联系管理员!");
        }
        return sendSuccess(0, "账号正常");
    }

    @RequestMapping(value = "queryVip")
    @ResponseBody
    public Object queryVip(Valid valid) {
        if (StringUtils.isBlank(valid.getImei())) {
            return sendFailed("非法访问!");
        }

        if (service.queryAccount(valid)) {
            return sendFailed("账号不存在!");
        }
        int code = service.query(valid);
        try {
            if (code == CODES) {
                return sendFailed(-1, "账号已被禁用");
            }
            if (code == CODE1) {
                return sendFailed(0, "账号未激活!");
            }
            if (code == CODE2) {
                return sendSuccess(1, "测试卡");
            }
            if (code == CODE3) {
                return sendSuccess(2, "周卡");
            }
            if (code == CODE4) {
                return sendSuccess(3, "月卡");
            }
            if (code == CODE5) {
                return sendSuccess(4, "终身VIP");
            }
        } catch (Exception e) {
            return sendFailed("服务器异常，请联系管理员!");
        }
        return sendFailed("非法操作!");
    }

    @RequestMapping("queryStatus")
    @ResponseBody
    public Object queryStatus(Valid valid) {
        if (StringUtils.isBlank(valid.getImei())) {
            return sendFailed("非法访问");
        }

        try {
            Valid status = service.findStutas(valid);
            if (!org.springframework.util.StringUtils.isEmpty(status)) {
                return sendSuccess(0, "ok", status);
            }
        } catch (Exception e) {
            return sendFailed("账号不存在!");
        }
        return sendFailed(-1, "请先激活设备");
    }


    @RequestMapping("version")
    @ResponseBody
    public Object version(){
        return sendSuccess(0,"ok",service.queryVersion());
    }
}
